# NFS filesystem mounting			-*- shell-script -*-

# FIXME This needs error checking

retry_nr=0

# parse nfs bootargs and mount nfs 
do_nfsmount()
{

	configure_networking

	# get nfs root from dhcp
	if [ "x${NFSROOT}" = "xauto" ]; then
		# check if server ip is part of dhcp root-path
		if [ "${ROOTPATH#*:}" = "${ROOTPATH}" ]; then
			NFSROOT=${ROOTSERVER}:${ROOTPATH}
		else
			ROOTSERVER=${ROOTPATH%:*}
			ROOTPATH=${ROOTPATH#*:}
			NFSROOT=${ROOTSERVER}:${ROOTPATH}			
		fi

	# nfsroot=[<server-ip>:]<root-dir>[,<nfs-options>]
	elif [ -n "${NFSROOT}" ]; then
		# nfs options are an optional arg
		if [ "${NFSROOT#*,}" != "${NFSROOT}" ]; then
			NFSOPTS="-o ${NFSROOT#*,}"
		fi
		NFSROOT=${NFSROOT%%,*}
		if [ "${NFSROOT#*:}" = "$NFSROOT" ]; then
			NFSROOT=${ROOTSERVER}:${NFSROOT}
		fi
	fi

	if [ -z "${NFSOPTS}" ]; then
		NFSOPTS="-o retrans=10"
	fi

	[ "$quiet" != "y" ] && log_begin_msg "Running /scripts/nfs-premount"
	run_scripts /scripts/nfs-premount
	[ "$quiet" != "y" ] && log_end_msg

	if [ ${readonly} = y ]; then
		roflag="-o ro"
	else
		roflag="-o rw"
	fi
	
	#########################################################################
        #               Agregado para ProRC
        #########################################################################
        echo 
        echo 
        echo 
        echo  "=================================================================="
        echo  "=                                                                ="
        echo  "=          PRotocolo de Reparto de Carga PRoRC                   ="
        echo  "=                                                                ="
        echo  "=================================================================="
        SERVER_ASIGN=`/sbin/socketclient ${ROOTSERVER} 1703 10`
        ERRSAL=$?

        if [ $ERRSAL -eq 0 ]; then
        	ROOTSERVER_ORI="${ROOTSERVER}"
        	ROOTSERVER="$SERVER_ASIGN"
        	echo  " Servidor DHCP: ${ROOTSERVER_ORI}       Path de Montaje: ${ROOTPATH}"
        	echo  " Servidor de Aplicaciones Designado SApD Ok: ${SERVER_ASIGN}."
        	echo  " Opciones de Montaje : ${NFSOPTS}		Punto de Montaje :${rootmnt}"
        	echo  "=================================================================="
		NFSROOT=${ROOTSERVER}:${ROOTPATH}
		nfsmount -o nolock ${roflag} ${NFSOPTS} ${NFSROOT} ${rootmnt}
		if [ $? -ne 0 ]; then
    			echo  "ERROR!  no se pudo montar del Servidor Designado por Reparto de Carga ${ROOTSERVER} !"
    			echo  "ERROR!  Continua por defecto  con ${ROOTSERVER_ORI} !"
    			echo  "=================================================================="
			sleep 3
    			ROOTSERVER_ORI=${ROOTSERVER}
			NFSROOT=${ROOTSERVER}:${ROOTPATH}
    			nfsmount -o nolock ${roflag} ${NFSOPTS} ${NFSROOT} ${rootmnt}
		fi
	else
		echo  " Servidor DHCP: ${ROOTSERVER}       Path de Montaje: ${ROOTPATH}"
                echo  " Servidor de Aplicaciones Designado SApD : ${SERVER_ASIGN}."
                echo  " Opciones de Montaje : ${NFSOPTS}         Punto de Montaje :${rootmnt}"
                echo  "=================================================================="
		echo  "ERROR! En Reparto de Carga !!!"
                NFSROOT=${ROOTSERVER}:${ROOTPATH}
                nfsmount -o nolock ${roflag} ${NFSOPTS} ${NFSROOT} ${rootmnt}
	fi
	sleep 10
}		


# NFS root mounting
mountroot()
{
	log_begin_msg "SCRIPT NFS ESTA BIEN"
	sleep 1
	log_end_msg
	[ "$quiet" != "y" ] && log_begin_msg "Running /scripts/nfs-top"
	run_scripts /scripts/nfs-top
	[ "$quiet" != "y" ] && log_end_msg

	modprobe nfs
	# For DHCP
	modprobe af_packet

	wait_for_udev 10

	# Default delay is around 180s
	# FIXME: add usplash_write info
	if [ -z "${ROOTDELAY}" ]; then
		delay=180
	else
		delay=${ROOTDELAY}
	fi

	# loop until nfsmount succeds
	while [ ${retry_nr} -lt ${delay} ] && [ ! -e ${rootmnt}${init} ]; do
		[ ${retry_nr} -gt 0 ] && \
		[ "$quiet" != "y" ] && log_begin_msg "Retrying nfs mount"
		do_nfsmount
		retry_nr=$(( ${retry_nr} + 1 ))
		[ ! -e ${rootmnt}${init} ] && /bin/sleep 1
		[ ${retry_nr} -gt 0 ] && [ "$quiet" != "y" ] && log_end_msg
	done

	[ "$quiet" != "y" ] && log_begin_msg "Running /scripts/nfs-bottom"
	run_scripts /scripts/nfs-bottom
	[ "$quiet" != "y" ] && log_end_msg
}
